/*
 *  client.c
 *  
 *
 *  Created by Viktor Kurz & Stefan Wrobel on 07.12.09.
 *  Copyright 2009. All rights reserved.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>

int main (int argc, const char * argv[]) {

	struct sockaddr_in server_addr;
	int len;
	int buflen;
	char buffer[255];
	int sock;
	unsigned int source_addr_len = sizeof(struct sockaddr);
	
	server_addr.sin_family = AF_INET;
	server_addr.sin_port   = htons(1234);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	
	if ((sock = socket( AF_INET, SOCK_DGRAM, 0)) == -1) {
		perror("fehler bei socket");
	}
	
	while (strcmp(buffer,"EXIT\0")!=0) {
		fgets (buffer , 255 , stdin);
		buflen = strlen(buffer);
		if((len=sendto (sock, buffer, buflen, 0,(struct sockaddr*) &server_addr, sizeof(struct sockaddr_in))) == -1){
			perror("fehler bei sendto");
		}
		if((len = recvfrom(sock, buffer, 255, 0, (struct sockaddr*) &server_addr, &source_addr_len)) == -1) {
			perror("Fehler bei Recvfrom!");
		}
		printf("%s\n", buffer);
	}
	
	
	return 0;
}